﻿using System;
using UnityEngine;
using UnityEngine.UI;

public class HorizontalScrollListCreator : HorizontalOrVerticalScrollListCreator
{
    public override void Init(ScrollRectEx tool, IScrollList scrollList)
    {
        base.Init(tool, scrollList);
        var layout = tool.content.GetComponent<HorizontalLayoutGroup>();
        spacing = layout.spacing;
        padding = layout.padding;
    }

    public override void SetNormalizePosition(float normalize)
    {
        var offset = tool.content.rect.width - tool.viewRect.rect.width;
        var pos = tool.content.anchoredPosition;
        pos.x = -offset * normalize;

        tool.SetContentAnchoredPositionSimple(pos);
    }

    #region Calculation

    protected override RectTransform NewBefore()
    {
        var item = PopItem();
        item.SetAsFirstSibling();

        m_refreshItemImmetiate(item);

        var offset = item.rect.width + spacing;
        var pos = tool.content.anchoredPosition;
        pos.x -= offset;

        tool.SetContentAnchoredPositionSimple(pos);

        return item;
    }

    protected override RectTransform NewNext()
    {
        var item = PopItem();
        item.SetAsLastSibling();
        return item;
    }

    protected override RectTransform RecycleBeginToEnd()
    {
        RectTransform go = activeList[0];
        m_recycleItem(0, go);

        go.SetAsLastSibling();

        var offset = go.rect.width + spacing;
        var pos = tool.content.anchoredPosition;
        pos.x += offset;

        tool.SetContentAnchoredPositionSimple(pos);

        return go;
    }

    protected override RectTransform RecycleEndToBegin()
    {
        var itemIndex = activeList.Count - 1;
        RectTransform item = activeList[itemIndex];
        m_recycleItem(itemIndex, item);

        item.SetAsFirstSibling();

        m_refreshItemImmetiate(item);

        var offset = item.rect.width + spacing;
        var pos = tool.content.anchoredPosition;
        pos.x -= offset;

        tool.SetContentAnchoredPositionSimple(pos);

        return item;
    }

    public override bool IsMoveNextDirection(Vector2 delta)
    {
        return delta.x < 0;
    }

    public override bool IsMoveBeforeDirection(Vector2 delta)
    {
        return delta.x > 0;
    }

    public override bool IsCanRecycleBegin()
    {
        if (activeList.Count == 0)
            return false;

        var item = activeList[0];
        var bounds = tool.GetBounds(item);
        if (bounds.max.x + spacing < tool.viewBounds.min.x)
            return true;

        return false;
    }

    public override bool IsCanRecycleEnd()
    {
        if (activeList.Count == 0)
            return false;

        var item = activeList[activeList.Count - 1];
        var bounds = tool.GetBounds(item);
        if (bounds.min.x - spacing > tool.viewBounds.max.x)
            return true;

        return false;
    }

    public override bool IsNeedCreateNext()
    {
        var offset = tool.viewBounds.max.x - tool.contentBounds.max.x + padding.right;
        if (offset > 0 || Mathf.Abs(offset) < offsetRange)
            return true;

        return false;
    }

    public override bool IsNeedCreateBefore()
    {
        var offset = tool.viewBounds.min.x - tool.contentBounds.min.x - padding.left;
        if (offset < 0 || Mathf.Abs(offset) < offsetRange)
            return true;

        return false;
    }

    #endregion

}